#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2023/04/12
# @Author  : Tuut
# @Email   : 49616026@qq.com
# @File    : MyPagerFactory.py

class MyPager:
    page_bar = {}
    page_bars = []

    def __init__(self):
        return self

    @staticmethod
    def make_pager(id, res, page_size, data_total, single_pager):
        pager = {"currentPage": 0, "lists": [], "total": 0, "pageSize": 0, "pages": [],
                 "pageSizeSelect": ['50', '100', '500']}
        _lists = []
        _total = int(0)
        if len(res) > 0:
            _tmp_list = []
            pages = [1]
            # 单页数据区分，所有数据在一页展示，或不需要进行分页处理
            if single_pager:
                if len(res) % page_size > 0:
                    _max = int(len(res) / page_size) + 1
                else:
                    _max = int(len(res) / page_size)
                for i in range(0, _max):
                    if i == _max:
                        _tmp_list.append(res[i * page_size:len(res) - i * page_size])
                    else:
                        _tmp_list.append(res[i * page_size:(i * page_size) + page_size])
                pager["lists"] = _tmp_list[0]
                pager["total"] = len(res)
            else:
                # 制作跳转页码
                if data_total > page_size:
                    pages = []
                    if data_total % page_size > 0:
                        for j in range(0, (int(data_total / page_size) + 1)):
                            pages.append(j + 1)
                    else:
                        for j in range(0, int(data_total / page_size)):
                            pages.append(j + 1)
                else:
                    pages = [1]
                pager["lists"] = res
                pager["total"] = data_total
            pager["pages"] = pages
            pager["pageSize"] = page_size
            pager["currentPage"]
        else:
            pager["lists"] = []
            pager["total"] = 0
            pager["pageSize"] = page_size
            pager["pages"] = [1]
            pager["currentPage"]=1
        if id not in MyPager.page_bars:
            MyPager.page_bar[id] = pager
            MyPager.page_bars.append(id)
        return pager
